package org.light.reflecttable.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.light.reflecttable.dao.FieldDao;
import org.light.reflecttable.domain.Field;
import org.light.utils.StringUtil;

public class FieldDaoImpl implements FieldDao{
	public List<Field> listAllTableFieldsByTableName(Connection connection, String tableName ,String dbName) throws Exception{
		try {
			String query = "SELECT column_name,column_comment,data_type, is_nullable, column_default, column_key, extra FROM information_schema.columns WHERE TABLE_SCHEMA = '"+dbName+"' and table_name = '"+StringUtil.changeDomainFieldtoTableColum(tableName)+"';";
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet result = ps.executeQuery();
			ArrayList<Field> list = new ArrayList<Field>();
			long serial = 1L;
			while(result.next()) {
				//Build the list object.
				Field field = new Field();
				field.setFieldType(result.getString("data_type"));
				field.setFieldNull(result.getString("is_nullable"));
				field.setFieldKey(result.getString("column_key"));
				field.setFieldDefault(result.getString("column_default"));
				field.setFieldExtra(result.getString("extra"));
				field.setFieldName(result.getString("column_name").toLowerCase());
				field.setLabel(result.getString("column_comment"));
				field.setActive(true);
				field.setId(serial);
				System.out.println("JerryDebug:field:"+field.getFieldName()+":"+field.getFieldNull()+":"+field.getFieldKey()+":"+field.getFieldDefault()+":"+field.getFieldExtra());
				//Build the object list.
				list.add(field);
				serial ++;
			}
			return list;
		} catch (Exception e){
			e.printStackTrace();
			return new ArrayList<Field>();
		}
	}


	@Override
	public List<Field> listAllPgTableFieldsByTableName(Connection connection, String tableName) throws Exception {
		try {
			String query = "SELECT column_name, data_type, is_nullable, column_default, description FROM information_schema.columns a left join pg_class c on relname=a.table_name LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = a.ordinal_position WHERE table_name = '"+ tableName +"';";
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet result = ps.executeQuery();
			ArrayList<Field> list = new ArrayList<Field>();
			long serial = 1L;
			while(result.next()) {
				//Build the list object.
				Field field = new Field();
				field.setFieldType(result.getString("data_type"));
				field.setFieldNull(result.getString("is_nullable"));
				field.setFieldDefault(result.getString("column_default"));
				field.setFieldName(result.getString("column_name").toLowerCase());
				field.setLabel(result.getString("description"));
				field.setActive(true);
				field.setId(serial);
				//Build the object list.
				list.add(field);
				serial ++;
			}
			return list;
		} catch (Exception e){
			e.printStackTrace();
			return new ArrayList<Field>();
		}
	}

}
